/**
 * @date: 2025/3/22
 * @author: 小红
 * @fileName: guard
 * @Description: 路由守卫
 */
import type { Router } from 'vue-router'
import { useAuthStore } from '@/stores/modules/auth.ts'

/**
 * 通用守卫配置
 * @param _router
 */
function setupCommonGuard(_router: Router) {
  // router.beforeEach(async (to) => {
  //   console.log(to)
  // })
}

/**
 * 权限访问守卫配置
 * @param router
 */
function setupAccessGuard(router: Router) {
  router.beforeEach(async (to, _from, next) => {
	document.title = to.meta?.title ? `Each-${to.meta.title}` : 'Each';
    const accessToken = useAuthStore().accessToken
    if (to.name === 'login' && accessToken) return next('/')

    if (!accessToken && to.name !== 'login') return next('/login')

    next()
  })
}

/**
 * 项目守卫配置
 * @param router
 */
export function createRouterGuard(router: Router) {
  /** 通用 */
  setupCommonGuard(router)
  /** 权限访问 */
  setupAccessGuard(router)
}
